package us.versus.them.hmap;

import flash.display.BitmapData;
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.text.TextField;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.KeyboardEvent;

class Ter extends Sprite {
	var map : HeightMap;
	var xOffset : Int;
	var yOffset : Int;
	var tileSize : Int;
	var depth: Int;

	public static function main() {
		var mapFile: String = Gordie.or(
			/* this will pull value from the flashvars in the html! */
			flash.Lib.current.loaderInfo.parameters.mapFile , "iso/maps/mandelbrot.gif" 
		);
		trace( " using " + mapFile );
		flash.Lib.current.addChild( new Ter( mapFile ) );
	}

	public function new ( mapLocation : String ) {
		super();
		this.gui1();
		this.reset();
		( new UriLoader( mapLocation ) ).load( terrainLoaded );
	}

	private function gui1() {
		this.textField = new TextField();
		this.textField.y = 100;
		this.addChild( textField );
		this.textField.text = "loading...";
	}

	public function reset() {
		this.xOffset = this.yOffset = 0;
		this.tileSize = 16 * 0 + 8;
		this.depth = tileSize;
	}

	public function terrainLoaded( uri : String, image : DisplayObject ) {
		this.map = new HeightMap( image );
		if ( false ) {
			// this will put the image on the screen
			super.addChild( image );
		}
		this.loadedx();
		//this.textField.textColor = 0xAAAA33;
		//this.textField.backgroundColor = 0x0;

		// this seems... kinda cumbersome...
		var root = flash.Lib.current;
		var params = root.loaderInfo.parameters;
		map.tileSize     = Std.parseInt( Gordie.or( params.tileSize, '' + 16                     ) );
		map.depth        = Std.parseInt( Gordie.or( params.depth   , '' + 16                     ) );
		map.screenWidth  = Std.parseInt( Gordie.or( params.width   , '' + root.stage.stageWidth  ) );
		map.screenHeight = Std.parseInt( Gordie.or( params.height  , '' + root.stage.stageHeight ) );
	}

	var textField : TextField;

	public function loadedx() {
		this.addChild( Gordie.newButton( " ", 20, 20, rs ) );
		this.textField.text = "click that blue thing...";
		flash.Lib.current.addEventListener( KeyboardEvent.KEY_DOWN, keyDown );
	}

	public function up( event:Dynamic ) { this.yOffset -= 2; this.draw(); }
	public function dn( event:Dynamic ) { this.yOffset += 2; this.draw(); }
	public function lt( event:Dynamic ) { this.xOffset -= 2; this.draw(); }
	public function rt( event:Dynamic ) { this.xOffset += 2; this.draw(); }

	public function rs( event:Dynamic ) { 
		//this.xOffset = this.yOffset = 0; 
		this.draw(); 
		this.textField.text = "you can move with\nthe arrow (or vi) keys";
	}

	public function click( event : Dynamic ) {
		if ( event.localX < 20 ) this.xOffset--; else this.xOffset++;
		if ( event.localY < 20 ) this.yOffset--; else this.yOffset++;
	}

	public function keyDown( event : Dynamic ) {
		switch ( event.keyCode ) {
			case 38: this.up( event );
			case 39: this.rt( event );
			case 40: this.dn( event );
			case 37: this.lt( event );
			case 75: return this.up( event );
			case 76: return this.rt( event );
			case 74: return this.dn( event );
			case 72: return this.lt( event );
			default: this.textField.text = "what does " + event.keyCode + " mean?";
		}
		return;
	}

	public function draw() {
		this.graphics.clear();
		this.textField.text = ( this.xOffset + "," + this.yOffset );
		this.map.draw( this.graphics, this.xOffset, this.yOffset );
	}
}
